﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.DAL.Models;

namespace SalesSys.BLL.Purchasing
{
    public interface IPurchasingBLL : IBaseBLL<PurchaseOrder, Guid>
    {


        /// <summary>
        /// 通过订单创建采购单
        /// </summary>
        /// <param name="createrId">创建人，采购员</param>
        /// <param name="order"></param>
        /// <remarks>
        /// 1. 
        /// 复制计划单创建采购单
        /// 计算采购单各商品标准价
        /// 更新计划单状态为：驻厂确认
        /// 关联采购单FromID
        /// 设置采购状态为：采购中
        /// 记录采购单变更历史
        /// </remarks>
        PurchaseOrder CreateBySale(Guid createrId, Order order);

        /// <summary>
        /// 根据计划单编号查询采购单
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        PurchaseOrder GetPurchaseOrderByOrderId(Guid orderId);


        /// <summary>
        /// 根据计划单修改采购单重量
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        int EditPurchaseOrderGoods(Order order);
    }

    public class PurchasingBLL : BaseBLL<PurchaseOrder, Guid>,IPurchasingBLL
    {
        private EmployeeBLL empBll = new EmployeeBLL();
        public PurchaseOrder CreateBySale(Guid createrId, Order order)
        {
            var porder = new PurchaseOrder();
            porder.Creater = empBll.Details(createrId);
            porder.FromId = order.Id;
            porder.PurchaseType = PurchaseType.Sale;
            porder.Provider = order.Provider;
            porder.ToId = order.Customer.Id;
            var list = new List<PurchaseGoods>();
            foreach (var item in order.CommodityInfos)
            {
                var goods = new PurchaseGoods();
                goods.Commodity = item.Commodity;
                goods.Count = item.Count;
                goods.Price = 0;
                goods.SourcePrice = 0;
                goods.SourceWeight = item.SourceWeight;
                goods.Weight = item.Weight;
                list.Add(goods);
            }
         
            porder.Goodses = list;
            porder.State = PurchaseOrder.PurchaseState.Pending;
            porder.CalcNew();
            Rep.Insert(porder);
            return porder;
        }



        /// <summary>
        /// 根据计划单编号查询采购单
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        public PurchaseOrder GetPurchaseOrderByOrderId(Guid orderId)
        {
            var stock = Rep.Entities.Where(p => p.FromId == orderId).FirstOrDefault();
            return stock;
        }

        /// <summary>
        /// 根据计划单修改采购单重量
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        public int EditPurchaseOrderGoods(Order order)
        {
            var pOrder = this.GetPurchaseOrderByOrderId(order.Id);

            foreach (var item in order.CommodityInfos)
            {
                var good = pOrder.Goodses.FirstOrDefault(p => p.Commodity.Id == item.Commodity.Id);
                if (good == null)
                {
                    throw new Exception("无法找到的订单商品信息");
                }
                good.Weight = item.Weight;
            }
           return Rep.Update(pOrder);
        }
    }
}
